package cn.sccdlg.crawler.jd.task;

import cn.sccdlg.crawler.jd.pojo.Item;
import cn.sccdlg.crawler.jd.service.ItemService;
import cn.sccdlg.crawler.jd.util.HttpUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @author RaoShuang
 * @emil 991207823@qq.com
 * @date 2020/11/26 22:49
 * 手机商品信息定时任务调度器
 */
@Component
public class ItemTask {

    @Autowired
    private HttpUtils httpUtils;
    @Autowired
    private ItemService itemService;

    private static final ObjectMapper MAPPER =  new ObjectMapper();

    //当下载任务完成后，间隔多长时间进行一次任务
    @Scheduled(fixedDelay = 300 * 1000)
    public void itemTask() throws Exception {
        //声明需要解析的初始地址，地址原例：https://list.jd.com/list.html?cat=9987%2C653%2C655&page=1&s=1&click=0
        String url = "https://list.jd.com/list.html?cat=9987%2C653%2C655&page=";
        //按照页面对手机的搜索结果进行遍历解析
        for (int i = 1, s = 1 ; i < 10; i = i + 2) {
            String html = httpUtils.doGetHtml(url + i + "&s=" + s + "&click=0");
            s = (i == 3) ? 57 : s+60;
            //解析页面，获取商品数据并存储
            if ("".equals(html)) {
                continue;
            }
            this.parse(html);
        }
        System.out.println("手机数据抓取成功");
    }

    /**
     * 解析页面，获取商品数据并存储
     */
    private void parse(String html) {
        //解析html获取Document
        Document doc = Jsoup.parse(html);
        //获取spu
        Elements spuEles = doc.select("div#J_goodsList > ul > li");
        for (Element spuEle : spuEles) {
            //获取spu信息
            String attr = spuEle.attr("data-spu");
            long spu = Long.parseLong("".equals(attr)? "0" : attr);
            Elements skuEles = spuEle.select("li.ps-item");
            for (Element skuEle : skuEles) {
                //获取sku
                long sku = Long.parseLong(skuEle.select("[data-sku]").attr("data-sku"));
                //根据sku查询商品数据
                Item item = new Item();
                item.setSku(sku);
                List<Item> list = this.itemService.findAll(item);
                //如果商品存在，就进行下一个循环，因为商品存在不需要再次保存
                if (list.size() > 0) {
                    continue;
                }
                //设置商品的spu
                item.setSpu(spu);
                //获取商品的详情url，https://item.jd.com/100004770249.html
                String itemUrl = "https://item.jd.com/" + sku + ".html";
                item.setUrl(itemUrl);
                //获取商品的图片
                String picUrl = "https:"+ skuEle.select("img[data-sku]").first().attr("data-lazy-img");
                String picName = "";
                if (!"https:".equals(picUrl)) {
                    //替换图片格式
                    picUrl = picUrl.replace("/n9/", "/n1/");
                    picName = this.httpUtils.doGetImage(picUrl);
                }
                item.setPic(picName);
                //获取商品的价格
                String priceJson = this.httpUtils.doGetHtml("https://p.3.cn/prices/mgets?skuIds=J_" + sku);
                double price = 0;
                try {
                    price = MAPPER.readTree(priceJson).get(0).get("p").asDouble();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                item.setPrice(price);
                //获取商品的标题
                String itemInfo = this.httpUtils.doGetHtml(item.getUrl());
                String title = Jsoup.parse(itemInfo).select("div#itemName").text();
                item.setTitle(title);
                //设置创建时间和更新时间
                item.setCreated(new Date());
                item.setUpdated(item.getCreated());
                //保存商品数据到数据库中
                this.itemService.save(item);
            }
        }
    }

}
